package uk.co.entropedia.display
{
	import com.bit101.components.Component;
	import com.bit101.components.HUISlider;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.UISlider;
	import com.bit101.components.VBox;
	import com.bit101.components.VUISlider;
	import com.bit101.components.Window;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	public class DialogSlider extends Window implements IDialogComponentEditor
	{
		protected var _target : UISlider;
		protected var dialogLabelInput : InputText;
		protected var maximumInput : InputText;
		protected var minimumInput : InputText;

		protected var margin : Number = 5;
		
		public function DialogSlider(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, title:String="Edit Slider")
		{
			super(parent, xpos, ypos, title);
			hasCloseButton = true;
			
			setSize(220,200);
			
			
			var dialogLayout : VBox = new VBox( this, margin, margin );			
			dialogLayout.setSize(width-margin,height-margin);
			dialogLayout.spacing = 5;
			dialogLabelInput = new InputText( dialogLayout, 0,0,'label',handleChange);
			new Label( dialogLayout, 0,0,'max');
			maximumInput = new InputText( dialogLayout, 0, 0, '', handleChange );
			new Label( dialogLayout, 0,0,'min');
			minimumInput = new InputText( dialogLayout, 0, 0, '', handleChange );
			visible = false;
			addEventListener( Event.CLOSE, handleClose );
		}
		
		public function set target(value:Component):void
		{
			_target = value as UISlider;
			visible = _target is UISlider;
			
			update();
		}
		
		public function get target():Component
		{
			return _target;
		}
		
		public function update():void
		{
			if( ! _target ) return;
			 dialogLabelInput.text = _target.label;
			 maximumInput.text = _target.maximum.toPrecision(10);
			 minimumInput.text = _target.minimum.toPrecision(10);
		}
		
		protected function handleChange(e:Event):void
		{
			_target.label = dialogLabelInput.text;
			_target.dispatchEvent( new Event( Event.RESIZE, true ) );
			_target.maximum = parseFloat( maximumInput.text ) || 0;
			_target.minimum = parseFloat( minimumInput.text ) || 0;
			_target.value = _target.minimum;
		}
		
		protected function handleClose(e:Event):void
		{
			visible = false;
		}
	}
}